<?php

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 * Description of quenmatkhau
 *
 * @author Tuyet
 */
class Quenmatkhau extends CI_Controller {
    

  function __construct() {
        parent::__construct();
        $this->load->model('m_dangky');
        $this->load->library(array("form_validation","email"));
    }
    
    function index(){
       $this->load->view('quenmatkhau');
    }
//    function create_random_string($num) {
//    //Tao du lieu cho hinh ngau nhien
//    $chars = array( 'a', 'A', 'b', 'B', 'c', 'C', 'd', 'D', 'e', 'E', 'f', 'F', 'g', 'G', 'h', 'H', 'i', 'I', 'j', 'J',  'k', 'K', 'l', 'L', 'm', 'M', 'n', 'N', 'o', 'p', 'P', 'q', 'Q', 'r', 'R', 's', 'S', 't', 'T',  'u', 'U', 'v', 'V', 'w', 'W', 'x', 'X', 'y', 'Y', 'z', 'Z', 
//                    '1', '2', '3', '4', '5', '6', '7', '8', '9','~','!','@','#','$','%','&','?');
//    $max_chars = count($chars) - 1;
//    for($i = 0; $i < $num; $i++) {
//      $code = ( $i == 0 ) ? $chars[rand(0, $max_chars)] : $code . $chars[rand(0, $max_chars)];
//    }
//    return $code;
// }

  
    function quenmatkhau1() {
        //echo "skgjlkg";
//         $this->form_validation->set_rules("email","Email","required|valid_email|callback_checkEmailForgot");
//         $data['error'] = "";
//        if($this->form_validation->run()==FALSE){
//
//           $this->load->view("../index.php/quenmatkhau");
//            
//       }else{
            $dk=new m_dangky();
            //$email = $this->input->post("fgemail");
            $fgemail = $_POST['fgemail'];
          //  echo  $fgemail;

            $info = $dk->Laythongtinquaemail($fgemail);
            foreach ($info as $value) {
                $Ten = $value->MaNguoiDung;
            }
            $password = rand(0,9).rand(0,9).rand(0,9).rand(0,9).rand(0,9);
          // var_dump($password);
           // echo $info;
            $reset = array(
                "MatKhau" => $password
            );
            //echo $Ten;
            
            if($dk->updateUser($reset, $Ten));else echo 'thanh cong';
//           echo $message = "Please login with :<br/>";
//           echo $message .= "username :" . $info['HoTen'] . "<br/>";
//           echo $message .= "password:" . $password;
           
           $config = array(
                    'protocol'    => 'smtp',
                    'smtp_host'    => 'ssl://smtp.gmail.com',
                    'smtp_port'   => '465',
                    'smtp_timeout' => '7',
                    'smtp_user'    => 'snow.staruit@gmail.com', // địa chỉ gmail
                    'smtp_pass'    => 'tuyet1211', // pass gmail
                    'charset'    => 'utf-8',
                    'newline'    => "\r\n",
                    'mailtype' => 'html', // or html
                    'validation' => TRUE, // bool whether to validate email or not      
           );
           $this->load->library('email',$config);
           $this->email->set_newline("\r\n");
           
           $this->email->from('snow.staruit@gmail.com', 'MBM');
           $this->email->to($fgemail);
           $this->email->subject('Reset mat khau');
           $this->email->message('Mat khau moi cua ban !!!');
           if($this->email->send()){
               echo "Mail da duoc gui cho ban!";
           }
           else {
               show_error($this->email->print_debugger());
           }
 

//            $this->load->library("my_email");
//            $this->my_email->config($mail);
//            $this->my_email->sendmail();
//
//            $this->set_userdata(array($this->_quenmatkhau => TRUE));
//            header("location:".base_url() );
// redirect(base_url()."home/user/fg_complete");
       // $this->load->view('quenmatkhau', $data);
        
    }

//----- Thong da gui mail sau khi báo là đã quen mat khau
        function fg_complete() {
            if ($this->session->userdata($this->_quenmatkhau) == TRUE) {
                $data['report'] = "Your email has been sending !";
                $this->my_layout->view("report", $data);
                $this->session->unset_userdata($this->_quenmatkhau);
            }/* else{
              redirect(base_url()."home/verify/login");
              } */
        }
}



?>
